package ${package.ServiceImpl};

import ${package.Entity}.${entity};
import ${package.Mapper}.${table.mapperName};
import ${package.Service}.${table.serviceName};
import com.jixiang.base.util.Assert;

import ${superServiceImplClassPackage};

import org.springframework.stereotype.Service;


import java.util.List;  

import cn.hutool.core.lang.Pair;
import cn.hutool.core.util.ObjectUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * ${table.comment!} 服务实现类
 *
 * @author ${author}
 * @since ${date}
 */
@Slf4j
@Service
public class ${table.serviceImplName} extends ${superServiceImplClass}<${table.mapperName}, ${entity}> implements ${table.serviceName} {

    /**
     * 增加${table.comment!?replace("表","")}
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void add(${entityNameUpperCase}DTO ${entityNameLowerCase}DTO) {
        ${entityNameUpperCase}Entity ${entityNameLowerCase}Entity = BeanUtil.sourceToTarget(${entityNameLowerCase}DTO, ${entityNameUpperCase}Entity.class);
        
        // TODO 判断${table.comment!?replace("表","")}是否已存在
        ${entityNameUpperCase}Entity dbEntity = this.getOne(Wrappers.lambdaQuery(${entityNameUpperCase}Entity.class));
        
        Assert.isFalse(ObjectUtils.isNotEmpty(dbEntity), "${table.comment!?replace("表","")}已存在,无法继续添加!");
    
        this.insert(${entityNameLowerCase}Entity);
    }

    /**
     * 编辑${table.comment!?replace("表","")}
     */
    @Transactional(rollbackFor = Exception.class)
    @Override
    public void update(${entityNameUpperCase}DTO ${entityNameLowerCase}DTO) {
        ${entityNameUpperCase}Entity ${entityNameLowerCase}Entity = BeanUtil.sourceToTarget(${entityNameLowerCase}DTO, ${entityNameUpperCase}Entity.class);

<#--        // TODO 判断${table.comment!?replace("表","")}是否已存在-->
<#--        ${entityNameUpperCase}Entity dbEntity = this.getOne(Wrappers.lambdaQuery(${entityNameUpperCase}Entity.class)-->
<#--                .ne(${entityNameUpperCase}Entity::getId, ${entityNameLowerCase}Entity.getId()));-->

<#--        Assert.isFalse(ObjectUtils.isNotEmpty(dbEntity), "${table.comment!?replace("表","")}已存在,无法继续添加!");-->
        
        this.updateById(${entityNameLowerCase}Entity);
    }

    /**
     * ${table.comment!?replace("表","")}详细接口
     */
    @Override
    public ${entityNameUpperCase}VO detail(${entityNameUpperCase}DTO ${entityNameLowerCase}DTO) {

        ${entityNameUpperCase}Entity ${entityNameLowerCase}Entity = this.getById(${entityNameLowerCase}DTO.getCode());
        Assert.isFalse(ObjectUtil.isEmpty(${entityNameLowerCase}Entity), "${table.comment!?replace("表","")}不存在!");

        ${entityNameUpperCase}VO ${entityNameLowerCase}VO = BeanUtil.sourceToTarget(${entityNameLowerCase}Entity, ${entityNameUpperCase}VO.class);
        
        // TODO设置附加信息 
        return ${entityNameLowerCase}VO;
    }

    /**
     * ${table.comment!?replace("表","")}列表查询接口
     */
    @Override
    public Pair<List<${entityNameUpperCase}VO>, Long> list(${entityNameUpperCase}DTO ${entityNameLowerCase}DTO) {
        Page<${entityNameUpperCase}Entity> page = new Page<>(${entityNameLowerCase}DTO.getPage(), ${entityNameLowerCase}DTO.getLimit());
    
        // 组装查询sql
        LambdaQueryWrapper<${entityNameUpperCase}Entity> queryWrapper = Wrappers.lambdaQuery(${entityNameUpperCase}Entity.class)
                .orderByDesc(${entityNameUpperCase}Entity::getCreationTime);
    
        Page<${entityNameUpperCase}Entity> pageData = this.page(page, queryWrapper);
    
        List<${entityNameUpperCase}Entity> records = pageData.getRecords();
        if (ObjectUtil.isEmpty(records)) return Pair.of(null, page.getTotal());
    
        // 返回数据
        List<${entityNameUpperCase}VO> returnList = BeanUtil.sourceToTarget(records, ${entityNameUpperCase}VO.class);
        // TODO设置附加信息                       
        return Pair.of(returnList, page.getTotal());
    }

    /**
     * 删除${table.comment!?replace("表","")}接口
     *
     * @param ${entityNameLowerCase}DTO
     */
    @Transactional(rollbackFor = Exception.class)                    
    @Override
    public void delete(${entityNameUpperCase}DTO ${entityNameLowerCase}DTO) {
<#--        List<Long> ids = ${entityNameLowerCase}DTO.getCodes();-->
<#--        this.removeByIds(ids);-->
    }
}
